package com.mkb.service.impl;

import java.util.List;

import com.mkb.entity.GraphUserDetailDO;
import com.mkb.entity.GraphUserDetailDTO;
import com.mkb.enums.SexEnum;
import com.mkb.utils.MapUtils;
import com.zz.common.util.ObjectUtils;
import com.zz.common.validator.Assert;
import com.zz.common.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mkb.dao.GraphUserDetailDAO;
import com.mkb.service.GraphUserDetailService;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class GraphUserDetailServiceImpl implements GraphUserDetailService {
	@Autowired
	private GraphUserDetailDAO graphUserDetailDAO;

	@Override
	public Boolean deleteById(Long id) {
		graphUserDetailDAO.deleteById(id);
		return true;
	}

	@Override
	public Long create(GraphUserDetailDTO record) throws Exception {
		GraphUserDetailDO entity = record.clone(GraphUserDetailDO.class);
		graphUserDetailDAO.insert(entity);
		return entity.getId();
	}

	@Override
	public GraphUserDetailDTO getById(Long id) throws Exception{
		Assert.isNull(id, "id 不能为空");
		GraphUserDetailDO entity= graphUserDetailDAO.getById(id);
		Assert.isNull(entity, "对象不存在");
		GraphUserDetailDTO dto = entity.clone(GraphUserDetailDTO.class);
		return dto;
	}

	@Override
	public Boolean update(GraphUserDetailDTO record) throws Exception {
		Assert.isNull(record, "对象不存在");
		Assert.isNull(record.getId(), "对象id不存在");
		GraphUserDetailDO entity = record.clone(GraphUserDetailDO.class);
		graphUserDetailDAO.update(entity);
		return true;
	}

	@Override
	public PageInfo<GraphUserDetailDTO> queryPage(GraphUserDetailDTO graphUserDetailDTO) throws Exception {
	        ValidatorUtils.validateProperty(graphUserDetailDTO, "pageNum", "pageSize");
		PageHelper.startPage(graphUserDetailDTO.getPageNum(), graphUserDetailDTO.getPageSize());
		List<GraphUserDetailDO> graphUserDetailDOS = graphUserDetailDAO.queryList(MapUtils.parseMap(graphUserDetailDTO));
		List<GraphUserDetailDTO> graphUserDetailDTOList = ObjectUtils.convertList(graphUserDetailDOS, GraphUserDetailDTO.class);
		for (GraphUserDetailDTO record : graphUserDetailDTOList){
			record.setSexStr(null == record.getSex()? "": SexEnum.getEnum(record.getSex()).getDesc());
		}
		return new PageInfo<>(graphUserDetailDTOList);
	}

	@Override
	public List<GraphUserDetailDTO> queryList(GraphUserDetailDTO graphUserDetailDTO) throws Exception {
		List<GraphUserDetailDO> graphUserDetailDOS = graphUserDetailDAO.queryList(MapUtils.parseMap(graphUserDetailDTO));
		List<GraphUserDetailDTO> graphUserDetailDTOList = ObjectUtils.convertList(graphUserDetailDOS, GraphUserDetailDTO.class);
		return graphUserDetailDTOList;
	}

	@Override
	public void createBatch(List<GraphUserDetailDTO> graphUserDetailList) throws Exception {
		graphUserDetailDAO.insertBatch(ObjectUtils.convertList(graphUserDetailList, GraphUserDetailDO.class));
	}

	@Override
	public void deleteByGraphId(Long graphId) throws Exception {
		graphUserDetailDAO.deleteByGraphId(graphId);
	}

}